package com.gopay.cashier.web.controller.scan;

import com.gopay.cashier.common.exception.WebException;
import com.gopay.cashier.common.util.qrcode.QRCodeUtil;
import com.gopay.cashier.web.command.ScanPayCommand;
import com.gopay.cashier.web.controller.BaseController;
import com.gopay.cashier.web.utils.CashierConfig;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.trans.TxnStaCode;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lij-jiang
 * @ClassName: WalletScanPayChannelController.java
 * @Description: 国付宝钱包扫码支付
 * @date 2017年12月01日 15:20
 */
@Controller(value = "walletScanPayChannelController")
@RequestMapping(value = "/scan")
public class WalletScanPayChannelController extends BaseController {

    protected static final String PORTAL_URL = CashierConfig.get(CashierConfig.PORTAL_URL);

    @Value("#{webProperties['wallet.app.url']}")
    private String walletAppUrl;

    /**
     * 国付宝钱包扫码
     * 由于在手机钱包会做校验 此处就不进行校验了。
     *
     * @return
     */
    @RequestMapping(value = "/walletPay")
    public ModelAndView walletPay(HttpServletRequest request, ScanPayCommand command) {
        ModelAndView modelAndView = new ModelAndView("/00100/content/scan/walletPay");

        String orderId = request.getParameter(GOPAY_ORDER_ID);

        try {
            CpsGenMainOrder order = getMainOrder(orderId, true);
            buildQrcode(order);
            modelAndView.addObject(GOPAY_ORDER_ID, orderId);
            modelAndView.addObject(GOPAY_ORDER_KEY, request.getParameter(GOPAY_ORDER_KEY));
            modelAndView.addObject("bankCode", command.getBankCode());
            modelAndView.addObject("qrCode", order.getRemark2());
            modelAndView.addObject("order", getOrderInfo(orderId));
        } catch (Exception e) {
            logger.error("国付宝钱包扫码异常：" + e);
        }
        return modelAndView;
    }

    private void buildQrcode(CpsGenMainOrder order) {
        String url;
        if (walletAppUrl.indexOf("?") > -1) {
            url = walletAppUrl + "&orderid=" + order.getGopayOrderId();
        } else {
            url = walletAppUrl + "?orderid=" + order.getGopayOrderId();
        }
        Map<String, Integer> size = new HashMap<>(2);
        size.put("width", 260);
        size.put("height", 260);
        //手机钱包端二维码参数规则
        String qRCode = QRCodeUtil.createQRCode(url, size);
        order.setRemark2(qRCode);
    }

    /**
     * 扫码支付 结果页面
     *
     * @param orderId
     * @return
     * @throws WebException
     */
    @RequestMapping(value = "/wallet/result")
    public ModelAndView expressResult(String orderId) throws WebException {
        CpsGenMainOrder order = cpsGeneralMainOrderQueryDAO.find(CpsGenMainOrder.class, orderId);
        if (order == null) {
            throw new WebException(ProcCodeConstants.PROC_CODE_100E5041);
        }

        if (TxnStaCode.TXN_STA_CD_30000.value == (order.getTxnStaCd())) {
            logger.error("订单状态不对！txn_sta_cd wrong!" + orderId);
            throw new WebException(ProcCodeConstants.PROC_CODE_100E5042);
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("portalUrl", PORTAL_URL);
        if (TxnStaCode.TXN_STA_CD_20000.value == order.getTxnStaCd()) {
            mv.setViewName("/00100/common/success");
        } else {
            mv.setViewName("/00100/common/fail");
        }
        mv.addObject("order", getOrderInfo(orderId));
        mv.addObject("notifyMerUrl", "/redirect/BankPayCallbackSelf.shtml?orderKey=" + getRequest().getParameter("orderKey") + "&orderId=" + orderId);
        return mv;
    }
}
